﻿#include "CCamerSdkOperEx.h"
#include <QCoreApplication>
#include "CHeader.h"
CCamerSdkOperEx::CCamerSdkOperEx(CDevDataCam * pDevDataCam):CCamerSdkOper(pDevDataCam)
{
}

CCamerSdkOperEx::~CCamerSdkOperEx()
{
	for (auto it = m_cMapVlc.begin(); it != m_cMapVlc.end(); it++)
	{
		if (it.value() != nullptr)
		{
			delete it.value();
			it.value() = nullptr;
		}
	}
	m_cMapVlc.clear();
	/*for (auto it = m_cMapRtsp.begin(); it != m_cMapRtsp.end(); it++)
	{
		if (it.value() != nullptr)
		{
			delete it.value();
			it.value() = nullptr;
		}
	}
	m_cMapRtsp.clear();*/
}


bool CCamerSdkOperEx::stopRealPlay(HWND hHand, QString & sReInfo)
{
	if (m_cMapVlc.contains(hHand))
	{
		bool bOk = false;
		CVlcOper* pVlcRtspOper = m_cMapVlc[hHand];
		QMetaObject::invokeMethod(QCoreApplication::instance(), [&bOk, pVlcRtspOper]()
			{
				bOk = pVlcRtspOper->stopCapture();
			}, Qt::BlockingQueuedConnection); // ⚠ 阻塞调用，等待执行完成
		if (bOk)
		{
			m_cMapVlcState[hHand] = false;
			delete pVlcRtspOper;
			m_cMapVlc[hHand] = nullptr;
		}
		m_cMapVlc.remove(hHand);
		sReInfo = A2T("关闭视频预览");
		return true;
	}
	else
	{
		sReInfo = A2T("关闭视频预览");
		return true;
	}
}

bool CCamerSdkOperEx::realPlay(int nDevId, HWND hHand, QWidget * pWidget, QString & sReInfo)
{
	QString sRtspUrl = "";
	if (nDevId == enIR)
	{
		sRtspUrl = m_pDevDataCam->m_sRtspUrlIr;
	}
	else if (nDevId == enCamer)
	{
		sRtspUrl = m_pDevDataCam->m_sRtspUrlImg;
	}
	else
	{
		sReInfo = "Device RtspUrl Null";
		return false;
	}
	if (m_cMapVlc.contains(hHand))
	{
		sReInfo = A2T("视频RTSP正在预览");
		return true;
	}
	else
	{
		bool bOk = false;
		CVlcOper*pVlcRtspOper = new CVlcOper(m_pDevDataCam,sRtspUrl);
		QMetaObject::invokeMethod(QCoreApplication::instance(), [&bOk, pWidget, pVlcRtspOper]()
			{
				pVlcRtspOper->setImageWidget(pWidget);
			    bOk = pVlcRtspOper->startCapture();  // 在主线程执行
			}, Qt::BlockingQueuedConnection); // ⚠️ 阻塞调用，等待执行完成
		if (bOk)
		{
			sReInfo = A2T("视频RTSP预览成功");
			m_cMapVlc.insert(hHand, pVlcRtspOper);
			m_cMapVlcState.insert(hHand, true);
			return true;
		}
		else
		{
			sReInfo = A2T("视频RTSP预览失败");
			delete pVlcRtspOper;
			pVlcRtspOper = nullptr;
			return false;
		}
	}
}

void CCamerSdkOperEx::setRollerNum(int nNum, int nBologn, QString sRollerDesc)
{
	for (auto it = m_cMapVlc.begin(); it != m_cMapVlc.end(); it++)
	{
		//it.value()->setRollerNum(nNum, nBologn, sRollerDesc);
	}
	/*for (auto it = m_cMapRtsp.begin(); it != m_cMapRtsp.end(); it++)
	{
		it.value()->setRollerNum(nNum, nBologn, sRollerDesc);
	}*/
}

void CCamerSdkOperEx::setCorrectData(double dLeftRectifyValue, double dRightRectifyValue, int nRectifySetValue, int nRectifySetValueTwo, QPointF BeltEdgePf1, QPointF BeltEdgePf2, QPointF BeltEdgePf3, QPointF BeltEdgePf4, int nIsHaveBeltState, bool bBeltState, int nDrawType)
{
	for (auto it = m_cMapVlc.begin(); it != m_cMapVlc.end(); it++)
	{
		//it.value()->setCorrectData(dLeftRectifyValue,dRightRectifyValue,nRectifySetValue,nRectifySetValueTwo,BeltEdgePf1,BeltEdgePf2,BeltEdgePf3,BeltEdgePf4,nIsHaveBeltState,bBeltState,nDrawType);
	}
	/*for (auto it = m_cMapRtsp.begin(); it != m_cMapRtsp.end(); it++)
	{
		it.value()->setCorrectData(dLeftRectifyValue, dRightRectifyValue, nRectifySetValue, nRectifySetValueTwo, BeltEdgePf1, BeltEdgePf2, BeltEdgePf3, BeltEdgePf4, nIsHaveBeltState, bBeltState, nDrawType);
	}*/
}

bool CCamerSdkOperEx::realPlayMainThread(int nDevId, HWND hHand, QWidget* pWidget, QString& sReInfo)
{
	return false;
}

